﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Midgard
{
    class Enemy
    {
        int health;
        Vector2 position, projectileVelocity;
        Texture2D enemySprite, projectileSprite;
        bool isActive;
        float speed;
        int projectileRate, projectileDamage;
        bool leftDirection;
        List<Projectile> projectiles; // Array list of projectiles
        Rectangle viewportRect;

        /* Temp Variables */
        int currentAttackCount;

        /* Used for animation purposes
        int height = 87;
        int frame, delay, currDelay;
        Rectangle destRect, sourceRect, destRect2;
        bool reverse;
        */

        public bool IsActive
        {
            get { return isActive; }
        }

        public int Health
        {
            get{return health;}
        }

        public Vector2 Position
        {
            get { return position; }
            
        }

        public float Speed
        {
            get { return speed; }
        }


        /* Enemy no animation */
        public Enemy(int EnemyHealth, Vector2 StartPosition, Texture2D EnemySpriteTexture, bool IsLeftDirection, 
            float EnemySpeed, Rectangle ViewportRectangle, Texture2D ProjectileSpriteTexture, Vector2 ProjectileVelocity,
            int ProjectileRate, int ProjectileDamage)
        {
            health = EnemyHealth;
            position = StartPosition;
            enemySprite = EnemySpriteTexture;
            leftDirection = IsLeftDirection;
            speed = EnemySpeed;
            viewportRect = ViewportRectangle;
            projectileSprite = ProjectileSpriteTexture;
            projectileVelocity = ProjectileVelocity;
            projectileRate = ProjectileRate;
            projectileDamage = ProjectileDamage;

            /* Initializing things */
            Initialize();


            /* Used for animation 
            frame = 0;
            destRect = new Rectangle((int)position.X, (int)position.Y, sprite.Width, height);
            sourceRect = new Rectangle(0, 0, sprite.Width, height);
            destRect2 = new Rectangle((int)position.X - Width, (int)position.Y, Width, height);
            delay = 6;
            reverse = false;
            */
        }

        public void Initialize()
        {
            isActive = true;
            projectiles = new List<Projectile>();
        }

        public void update(GameTime gameTime)
        {
            // Checks the health
            if (health <= 0)
                isActive = false;

            /* Delete enemies if they go out of the boundary */
            if ( (position.X < 0 && leftDirection) || (position.X > viewportRect.Width && !leftDirection) )
                isActive = false;


            /* Constant Motion */
            if (leftDirection)
                position = new Vector2(position.X + speed, position.Y);
            else
                position = new Vector2(position.X - speed, position.Y);


            /* Attack rate */
            /* If the current attack count is larger than the projectile rate, then the enemy can attack */
            if (currentAttackCount > projectileRate)
            {
                projectiles.Add(new Projectile(projectileSprite, new Vector2(enemySprite.Width / 2, position.Y),
                    projectileVelocity, viewportRect, projectileDamage, 0f, Projectile.Faction.enemy));
            }


            /* Update the Projectiles */
            for (int i = projectiles.Count - 1; i >= 0; i--)
            {
                projectiles[i].Update();

                if (projectiles[i].active == false)
                {
                    projectiles.RemoveAt(i);
                }
            }


            /* Should update the source and destination rectangles as well 
            destRect = new Rectangle((int)position.X, (int)position.Y, Width, height);
            sourceRect = new Rectangle(0, height * frame, sprite.Width, height);
            destRect2 = new Rectangle((int)position.X - Width, (int)position.Y, Width, height);
            currDelay++;

            if (currDelay > delay)
            {
                if (reverse)
                {
                    frame = (frame - 1);
                    if (frame == 0)
                        reverse = false;
                }
                else
                {
                    frame = (frame + 1) % 4;
                    if (frame == 3)
                        reverse = true;
                }



                currDelay = 0;
            }
            */
        }


        public void draw(SpriteBatch spriteBatch)
        {
             spriteBatch.Draw(enemySprite, position, Color.White);
            //spriteBatch.Draw(enemySprite, destRect, sourceRect, Color.White);

            for (int i = 0; i < projectiles.Count; i++)
            {
                projectiles[i].Draw(spriteBatch);
            }
        }
    }
}
